package com.mjk.common.web.getway.restful.interceptor.type.export;


import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.interceptor.DefaultInterceptorChain;
import com.mjk.common.util.RandomUtil;
import com.mjk.common.web.getway.constns.GetWayError;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.type.export.consts.ExportProcessEnum;
import com.mjk.common.web.getway.restful.interceptor.type.export.impl.ExportProcessInterceptor;
import com.mjk.common.web.getway.restful.interceptor.InterceptorInvoker;
import com.mjk.common.web.getway.utils.ResponseUtils;

/**
 * 公用网关执行器
 * @author mjk
 *
 */
public class ExportInterceptorInvoker implements InterceptorInvoker {

	
	@Override
	public BeeResponse invoker(DefaultInterceptorChain<GetWayInterceptorReq, BeeResponse> chains,
							   GetWayInterceptorReq req) {
			//文件名称
			String uuid = RandomUtil.getShortUuid();
			if(!req.getParams().containsKey(ExportProcessInterceptor.FILE_NAME_KEY)){
				req.getParams().put(ExportProcessInterceptor.FILE_NAME_KEY, uuid+"_"+req.getFileName());
			}else{
				req.getParams().put(ExportProcessInterceptor.FILE_NAME_KEY, uuid+"_"+req.getParams().get(ExportProcessInterceptor.FILE_NAME_KEY));
			}//是否是下载
			if(ExportProcessEnum.isDown(req)){
				BeeResponse result = chains.proceed(req);
				if(result.getCode().intValue() == GetWayError.SUC.getCode().intValue()){
					 ResponseUtils.downFile(req.getResponse(),result,req.getUploadPath()+req.getParams().get(ExportProcessInterceptor.FILE_NAME_KEY));
				}else{
					 ResponseUtils.returnGetWay(req.getResponse(),result);
				}
			}else{//过程
				 ResponseUtils.returnGetWay(req.getResponse(),chains.proceed(req));
			}
			return GetWayError.SUC.tranceError("");
	}
}